﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.IO;


namespace HRdex_Final
{
    public partial class WebForm3 : System.Web.UI.Page
    {
        SqlConnection conn = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\HRdexDB.mdf;Integrated Security=True;User Instance=True");

        protected void Page_Load(object sender, EventArgs e)
        {
            string JobID = Request.QueryString["JobID"];
            if (JobID != null)
            {
            SqlCommand cmdJobSelect = new SqlCommand("SELECT JobID, Title from catJobTitle WHERE (JobID = '" + JobID+"')", conn);
            SqlDataAdapter daJobSelect = new SqlDataAdapter(cmdJobSelect);
            DataTable dtJob = new DataTable("dtJob");
            conn.Open();
            daJobSelect.Fill(dtJob);

                ddlJobTitle.SelectedValue = dtJob.Rows[0].ItemArray[0].ToString();
                ddlJobTitle.Enabled = false;
                conn.Close();
            }
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            SqlCommand cmd = new SqlCommand("INSERT INTO formApplicant(fname, lname, address, contactnum, email, birthday,jobID, pubdate, resume, resType, resName) " +
                "VALUES(@fname,@lname,@address,@conNum,@email,@bday, @job, @pdate, @resume, @resType, @resName)", conn);
            SqlDataAdapter da = new SqlDataAdapter(cmd);

            conn.Open();
            Stream fs = fuResume.PostedFile.InputStream;
            BinaryReader br = new BinaryReader(fs);
            Byte[] bytes = br.ReadBytes((Int32)fs.Length);

            string filePath = fuResume.PostedFile.FileName;
            string fileType = Path.GetExtension(filePath);
            string fileName = Path.GetFileName(filePath);

            cmd.Parameters.Add("@fname", SqlDbType.VarChar).Value = txtFirstNAme.Text;
            cmd.Parameters.Add("@lname", SqlDbType.VarChar).Value = txtLastName.Text;
            cmd.Parameters.Add("@address", SqlDbType.VarChar).Value = txtAddress.Text;
            cmd.Parameters.Add("@conNum", SqlDbType.VarChar).Value = txtContact.Text;
            cmd.Parameters.Add("@email", SqlDbType.VarChar).Value = txtEmail.Text;
            cmd.Parameters.Add("@bday", SqlDbType.VarChar).Value = txtbday.Text;
            //cmd.Parameters.Add("@resume", SqlDbType.VarChar).Value = fuResume.FileContent();
            cmd.Parameters.Add("@job", SqlDbType.Int).Value = ddlJobTitle.SelectedValue;
            cmd.Parameters.Add("@pdate", SqlDbType.DateTime).Value = DateTime.Now;
            cmd.Parameters.Add("@resume", SqlDbType.Binary).Value = bytes;
            cmd.Parameters.Add("@resType", SqlDbType.VarChar).Value = fileType;
            cmd.Parameters.Add("@resName", SqlDbType.VarChar).Value = fileName;

            cmd.ExecuteNonQuery();
            conn.Close();
            lblConfirmation.Text = "You successfully apply at Adamson University. Please wait for an email within the day as we review your qualification. Thank you!";

            txtAddress.Visible = false;
            txtContact.Visible = false;
            txtEmail.Visible = false;
            txtFirstNAme.Visible= false;
            txtLastName.Visible = false;
            txtbday.Visible= false;
            ddlJobTitle.Visible = false;
            fuResume.Visible = false;
            btnSubmit.Visible = false;
        }

        public void Select()
        {

            //SqlCommand cmdSelect = new SqlCommand("SELECT fname, lname, address, contactnum, email, birthday, jobID, pubdate from FromApplicant");
            //SqlDataAdapter daSelect = new SqlDataAdapter(cmdSelect);
            //DataTable dt = new DataTable("dt");

            //daSelect.Fill(dt);

            //txtFirstNAme.Text = dt.Rows[0].ItemArray[0].ToString();
            //txtLastName.Text = dt.Rows[0].ItemArray[1].ToString();
            //txtAddress.Text = dt.Rows[0].ItemArray[2].ToString();
            //txtContact.Text = dt.Rows[0].ItemArray[3].ToString();
            //txtEmail.Text = dt.Rows[0].ItemArray[4].ToString();
            //dpBirthday.SelectedDateString = dt.Rows[0].ItemArray[5].ToString();
            //lblPubDate.Text = dt.Rows[0].ItemArray[7].ToString();
        }

    }
}